<template>
  <el-container class="frame" style="height: 100%">
    <!-- 侧边栏 -->
    <el-aside :width="isMobile? '0' : (isCollapse? '64px' : '240px')" class="my-aside">
      <div class="aside-header">
        <img src="@/assets/images/scau-logo.png" alt="Logo" class="logo-img" />
        <h5 v-if="!isCollapse && !isMobile" class="logo">SCAU - 物业管理系统</h5>
      </div>
      <el-menu
          :collapse="isCollapse"
          class="menu-style"
          default-active="statistics-users"
          @select="pageChange"
      >
        <template v-if="!isCollapse || isMobile">
          <el-menu-item class="side-menu-item" index="personal-center" @click="pageChange('p-personal-center')">个人中心</el-menu-item>
          <el-menu-item class="side-menu-item" index="owner-info" @click="pageChange('owner-info')">业主信息</el-menu-item>
          <el-menu-item class="side-menu-item" index="notice-management" @click="pageChange('notice-management')">通知管理</el-menu-item>
          <el-menu-item class="side-menu-item" index="property-fee-collection" @click="pageChange('property-fee-collection')">缴费管理</el-menu-item>
          <el-menu-item class="side-menu-item" index="repair-handling" @click="pageChange('repair-handling')">维修处理</el-menu-item>
          <el-menu-item class="side-menu-item" index="complaint-suggestion-handling" @click="pageChange('complaint-suggestion-handling')">投诉与建议处理</el-menu-item>
          <el-menu-item class="side-menu-item" index="try" @click="pageChange('parkings-management')">车位管理</el-menu-item>
        </template>
      </el-menu>
    </el-aside>

    <el-container style="width: 100%">
      <!-- 顶部栏 -->
      <el-header class="my-top-nav" :style="{ left: headerLeft }">
        <div style="display: flex; align-items: center;">
          <el-button text @click="toggleMenu">
            <el-icon>
              <component :is="isCollapse? Expand : Fold" />
            </el-icon>
            <h5 style="margin-left: 10px">物业端</h5>
          </el-button>
        </div>

        <div class="user-info">
          <span style="margin-right: 10px;">欢迎你, {{ userName }}</span>
          <el-button type="info" round @click="handleLogout">退出登录</el-button>
        </div>
      </el-header>

      <!-- 主体内容 -->
      <el-main class="main-content" :style="{ marginLeft: mainContentMarginLeft }">
        <router-view class="content-area" />
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import axios from 'axios';
import { Expand, Fold, Menu } from "@element-plus/icons-vue";
import { computed, onMounted, ref } from "vue";
import { useRouter } from "vue-router";

export default {
  name: "PropertyHome",
  computed: {
    Fold() {
      return Fold;
    },
    Expand() {
      return Expand;
    },
    mainContentMarginLeft() {
      return this.isMobile? '0px' : (this.isCollapse? '64px' : '240px');
    }
  },
  components: {
    [Menu.name]: Menu,
    [Fold.name]: Fold,
    [Expand.name]: Expand,
  },
  data() {
    return {
      userName: localStorage.getItem('userName') || '',
    };
  },
  created() {
    this.fetchPersonalInfo();
  },
  setup() {
    const isCollapse = ref(false);
    const isMobile = ref(false);
    const router = useRouter();
    const userName = ref(localStorage.getItem('userName') || '');

    const checkScreen = () => {
      isMobile.value = window.innerWidth < 768;
    };

    const toggleMenu = () => {
      isCollapse.value = !isCollapse.value;
    };

    const headerLeft = computed(() => {
      if (isMobile.value) return '0px';
      return isCollapse.value? '64px' : '240px';
    });

    onMounted(() => {
      checkScreen();
      window.addEventListener('resize', checkScreen);
    });

    return {
      userName,
      isCollapse,
      isMobile,
      toggleMenu,
      headerLeft,
      pageChange(url) {
        router.push(url);
      },
      handleLogout() {
        localStorage.clear();
        router.push('/');
      },
    };
  },
  methods: {
    async fetchPersonalInfo() {
      try {
        // 从 localStorage 中获取 token
        const token = localStorage.getItem('token');
        if (!token) {
          console.log('token 不存在');
          return;
        }

        // 发送请求获取个人信息
        const response = await axios.get('/api/users/personal', {
          headers: {
            Authorization: token,
          },
        });

        if (response.data.code === 200) {
          // 存储用户姓名
          this.userName = response.data.data.name;
          localStorage.setItem('userName', this.userName);
          console.log('用户姓名已存储到 localStorage', this.userName);
          const communityId = response.data.data.communityId;
          localStorage.setItem('communityId', communityId);
        } else {
          console.log('获取个人信息失败', response.data.msg);
        }
      } catch (error) {
        console.error('请求出错', error);
      }
    },
    pageChange(url) {
      this.$router.push(url);
    },
    handleLogout() {
      localStorage.removeItem('userId');
      localStorage.removeItem('token');
      localStorage.removeItem('userName');
      this.$router.push('/');
      console.log('执行退出登录操作');
    },
  },
};
</script>

<style scoped>
/* 添加的CSS样式 */
.el-main.main-content {
  display: flex;
  justify-content: center; /* 水平居中 */
  align-items: center; /* 垂直居中 */
  height: 100%; /* 确保占满整个主区域 */
  transition: margin-left 0.3s; /* 过渡效果 */
}

.content-area-container {
  width: 100%; /* 或根据需要调整宽度 */
  max-width: 1200px; /* 可选：限制最大宽度 */
  padding: 20px; /* 可选：添加内边距 */
}

.logo {
  color: white;
  text-align: center;
}

.side-menu-item {
  background-color: #304055;
  color: white; /* 设置字体颜色为白色 */
}

/* 白色箭头 */
::v-deep(.el-sub-menu__icon-arrow) {
  color: white!important;
}

::v-deep(.el-sub-menu__title:hover),
::v-deep(.el-menu-item:hover) {
  background-color: black!important;
  color: white!important;
}

.my-aside {
  height: 100vh;
  background-color: #304055;
  overflow: hidden; /* 防止整体侧边栏多余部分显示 */
  transition: width 0.3s;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 999;
  display: flex;
  flex-direction: column;
}

.aside-header {
  height: 50px;
  display: flex;
  align-items: center;
  padding: 0 10px;
  flex-shrink: 0;
  background-color: #2d3747;
}

/* 滚动容器，让菜单可以滚动 */
.aside-scroll {
  flex: 1;
  overflow-y: auto;
}

/* 可选：自定义滚动条样式 */
.aside-scroll::-webkit-scrollbar {
  width: 4px;
}

.aside-scroll::-webkit-scrollbar-thumb {
  background-color: rgba(255, 255, 255, 0.3);
  border-radius: 4px;
}

.logo-img {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: white;
  margin-right: 10px;
}

.my-top-nav {
  position: fixed;
  top: 0;
  right: 0;
  height: 50px;
  padding: 0 20px;
  background-color: white;
  border-bottom: 1px solid #e5e4e4;
  display: flex;
  justify-content: space-between;
  align-items: center;
  z-index: 1000;
  transition: left 0.3s ease;
}

.content-area {
  background-color: #f2f5fa;
  padding: 0;
  height: auto; /* 根据内容自动调整高度 */
  overflow-y: auto; /* 启用滚动条（仅在内容超出时显示） */
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); /* 添加阴影效果 */
  width: 100%; /* 确保宽度占满 */
}

/* 响应式适配 */
@media (max-width: 768px) {
  .main-content,
  .my-aside {
    width: 0!important;
  }

  .logo {
    display: none;
  }
}

.container-style {
  height: 100%;
}

.header-style {
  background-color: rgb(36, 51, 67);
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  color: white;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 15px;
}

.footer-style {
  text-align: center;
  background-color: rgb(36, 51, 67);
  color: white;
}

.content-area {
  flex: 1;
  padding: 20px;
  background-color: #F5F8FD; /*信息显示的背景色*/
}

.menu-aside {
  width: 200px;
  background-color: #243343;
}

.menu-style {
  background-color: #2C3E50;
  border-right: none;
  color: white; /*文字颜色*/
}

.el-menu-item {
  color: white; /*菜单项文字颜色*/
}

/* 鼠标移入时改变背景色 */
.el-menu-item:hover {
  background-color: rgba(20, 146, 174, 0.68);
}
</style>